<?php
class sdk_user_model extends CI_Model {
    public function __construct() {
        $this->sws = $this->load->database ( 'sws', TRUE );
    }
    
    /**
     *
     * 查询用户信息
     *
     * @name query_user_by_account
     * @param string $account            
     * @author Eifel.Deng 2015年3月19日
     */
    public function query_user_by_account($account) {
        $account = $this->sws->escape ( $account );
        $sql = "select 
                     USER_ID,PASSWORD,DEVICE_IMEI,REG_TIME,BIND_PHONE,BIND_PHONE_MESSAGE_CODE   
                from sdk_user 
                where ACCOUNT_NO =$account 
                limit 1";
        $query = $this->sws->query ( $sql );
        if ($query->num_rows () > 0) {
            return $query->row_array ();
        }
    }
    public function query_user_by_id($user_id) {
        $user_id = $this->sws->escape ( $user_id );
        $sql = "select 
                     ACCOUNT_NO,PASSWORD,DEVICE_IMEI,REG_TIME,SESSION_KEY  
                from sdk_user 
                where USER_ID =$user_id 
                limit 1";
        $query = $this->sws->query ( $sql );
        if ($query->num_rows () > 0) {
            return $query->row_array ();
        }
    }
    
    /**
     *
     * 输入注册帐号
     *
     * @param array $user_data            
     */
    public function insert_new_user($user_data) {
        $data = array (
                'ACCOUNT_NO' => $user_data ['account_no'],
                'PASSWORD' => $user_data ['password'],
                'DEVICE_IMEI' => $user_data ['device_imei'],
                'PHONE_NUM' => $user_data ['phone_num'],
                'REG_TIME' => $user_data ['reg_time'],
                'REG_MERCHANT_ID' => $user_data ['mer_id'] 
        );
        
        $this->sws->insert ( "sdk_user", $data );
        return $this->sws->insert_id ();
    }
    
    /**
     *
     * 插入用户登录日志
     *
     * @name insert_user_log
     * @param array $data            
     * @author Eifel.Deng 2015年3月19日
     */
    public function insert_user_log($data) {
        $this->sws->insert ( "sdk_user_log", $data );
    }
    /**
     *
     * 插入用户open_id
     *
     * @name insert_merchant_user_open_id
     * @param unknown $user_id            
     * @param unknown $mer_id            
     * @param unknown $open_id            
     * @return unknown
     * @author Eifel.Deng 2015年3月19日
     */
    public function insert_merchant_user_open_id($user_id, $mer_id, $open_id) {
        $data = array (
                "OPEN_ID" => $open_id,
                "USER_ID" => $user_id,
                "MERCHANT_ID" => $mer_id 
        );
        $this->sws->insert ( "sdk_merchant_user", $data );
        return $open_id;
    }
    /**
     *
     * 更新用户token
     *
     * @name update_user_token
     * @param unknown $open_id            
     * @param unknown $access_token            
     * @author Eifel.Deng 2015年3月19日
     */
    public function update_user_token($open_id, $access_token, $user_id, $mer_id, $login_date) {
        $data = array (
                "ACCESS_TOKEN" => $access_token,
                "OPEN_ID" => $open_id,
                "LAST_LOGIN_TIME" => $login_date,
                "USER_ID" => $user_id,
                "MERCHANT_ID" => $mer_id 
        );
        $this->sws->insert ( "sdk_merchant_user", $data );
    }
    
    /**
     *
     * 查询用户token
     *
     * @name query_user_access_token
     * @param unknown $open_id            
     * @return string token
     * @author Eifel.Deng 2015年3月19日
     */
    public function query_user_access_token($open_id) {
        $open_id = $this->sws->escape ( $open_id );
        $sql = "select
                        ACCESS_TOKEN
                from sdk_merchant_user
                where OPEN_ID =$open_id 
                limit 1";
        $query = $this->sws->query ( $sql );
        if ($query->num_rows ()) {
            $row = $query->row_array ();
            return $row ['ACCESS_TOKEN'];
        }
    }
    /**
     *
     * 修改密码
     *
     * @name change_password
     * @param unknown $account_no            
     * @param unknown $password            
     * @author Eifel.Deng 2015年3月19日
     */
    public function change_password($account_no, $password) {
        $data = array (
                "PASSWORD" => $password 
        );
        $this->sws->where ( "ACCOUNT_NO", $account_no );
        $this->sws->update ( "sdk_user", $data );
    }
    public function clear_user_token($open_id) {
        $sql = "delete
                from sdk_merchant_user
                where OPEN_ID = '$open_id'
                limit 1";
        $this->sws->query ( $sql );
    }
    
    /**
     *
     * 更新登录信息
     *
     * @name update_user_login_info
     * @param unknown $user_id            
     * @param unknown $mer_id            
     * @param unknown $date_time            
     * @author Eifel.Deng 2015年3月20日
     */
    public function update_user_login_info($user_id, $mer_id, $date_time) {
        $data = array (
                "LAST_LOGIN_TIME" => $date_time,
                "LAST_LOGIN_MERCHANT" => $mer_id 
        );
        $this->sws->where ( "USER_ID", $user_id );
        $this->sws->update ( "sdk_user", $data );
    }
    /**
     *
     * 更新用户的绑定码
     *
     * @name update_user_bind_message_code
     * @param unknown $account_no            
     * @param unknown $phone_num            
     * @param unknown $code            
     * @author Eifel.Deng 2015年3月27日
     */
    public function update_user_bind_message_code($account_no, $phone_num, $code) {
        $data = array (
                "BIND_PHONE_MESSAGE_CODE" => $code,
                "PHONE_NUM" => $phone_num 
        );
        $this->sws->where ( "ACCOUNT_NO", $account_no );
        $this->sws->update ( "sdk_user", $data );
    }
    /**
     *
     * 更改用户绑定手机的状态为1
     *
     * @name user_bind_phone_num
     * @param unknown $account_no            
     * @author Eifel.Deng 2015年3月27日
     */
    public function user_bind_phone_num($account_no) {
        $data = array (
                "BIND_PHONE" => 1 
        );
        $this->sws->where ( "ACCOUNT_NO", $account_no );
        $this->sws->update ( "sdk_user", $data );
    }
}




